﻿@page "/hot"

<div class="hot-container">
    @if (hotnews == null)
    {
        <Loading />
    }
    else
    {
        <div class="plus-container">
            <div class="hot-nav">
                @if (sources != null)
                {
                    @foreach (var item in sources.Result)
                    {
                        <NavLink class="@(item.Value == sourceId ? "hot-active" : null)" @onclick="(async () => await ChangeSource(item.Value))">@item.Description</NavLink>
                    }
                }
            </div>
            <div class="hot-content plus-row plus-col-space20">
                <ul>
                    @foreach (var item in hotnews.Result)
                    {
                        <li>
                            <NavLink target="_blank" href="@item.Url">@(string.IsNullOrEmpty(item.Title) ? item.Url : item.Title)</NavLink>
                        </li>
                    }
                </ul>
            </div>
        </div>
    }
</div>

@code {
    /// <summary>
    /// 来源Id
    /// </summary>
    private int sourceId { get; set; }

    /// <summary>
    /// 每日热点来源
    /// </summary>
    private ServiceResult<IEnumerable<EnumResponse>> sources;

    /// <summary>
    /// 每日热点列表
    /// </summary>
    private ServiceResult<IEnumerable<HotNewsDto>> hotnews;

    /// <summary>
    /// 初始化
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await Common.SetTitleAsync("每日热点");

        var uri = await Common.CurrentUri();
        var query = QueryHelpers.ParseQuery(uri.Query);

        var hasSourceId = query.TryGetValue("s", out Microsoft.Extensions.Primitives.StringValues s);

        if (!int.TryParse(s, out int _sourceId) || !hasSourceId)
        {
            _sourceId = 1;
        }

        sourceId = _sourceId;

        await SettingRouteAsync();
        await GetHotNewsSourceAsync();
        await QueryHotNewsAsync();
    }

    /// <summary>
    /// 获取每日热点来源
    /// </summary>
    /// <returns></returns>
    private async Task GetHotNewsSourceAsync()
    {
        sources = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<EnumResponse>>>("/hotnews/sources");
    }

    /// <summary>
    /// 根据来源获取每日热点列表
    /// </summary>
    /// <returns></returns>
    private async Task QueryHotNewsAsync()
    {
        hotnews = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<HotNewsDto>>>($"/hotnews?sourceId={sourceId}");
    }

    /// <summary>
    /// 来源点击事件
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    private async Task ChangeSource(int value)
    {
        if (value == sourceId) return;

        hotnews = null;
        sourceId = value;

        await SettingRouteAsync();
        await QueryHotNewsAsync();
    }

    /// <summary>
    /// 设置路由
    /// </summary>
    /// <returns></returns>
    private async Task SettingRouteAsync()
    {
        await Common.NavigateTo($"/hot?s={sourceId}");
    }
}